<?php
/**
 * 后台文章类别管理的curd
 * Author: lyun
 * Date: 2018/1/26
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin;

use App\Http\Models\ArticleTag;
use App\Http\Requests\Admin\ArticleTypeStoreRequest;
use App\Http\Requests\Admin\ArticleTypeUpdateRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleTypeController extends Controller
{
    use ApiResponser,ApiChecker;

    /**
     * 文章类别的列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tags = ArticleTag::where('super_tag_id',ArticleTag::TOP_TAG)
            ->with('getSubTags')
            ->latest('sort')
            ->get();

        return $this->response(compact('tags'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 返回一级种类的数组
        $super_tags = ArticleTag::getSupArticleType();

        return $this->response(compact('super_tags'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ArticleTypeStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleTypeStoreRequest $request)
    {
        $article_type_data = $this->transformArticleTypeData($request);

        $stored = ArticleTag::create($article_type_data);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ArticleTypeUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ArticleTypeUpdateRequest $request, $id)
    {
        /** @var ArticleTag $article_type */
        $article_type = ArticleTag::find($id);
            $this->checkItemExist($article_type, '文章的类别名');

        $article_type_data = $this->transformArticleTypeData($request);

        $updated = $this->updateArticleType($article_type_data,  $article_type);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        /** @var ArticleTag $article_type */
        $article_type = ArticleTag::find($id);
            $this->checkItemExist($article_type, '文章的类别名');

        $deleted = $this->deleteProductCategory($article_type);

        return $this->response(compact('deleted'));
    }

    /**
     * @param ArticleTag $article_type
     * @return bool|null
     */
    private function deleteProductCategory(ArticleTag $article_type)
    {
        if (($article_type->super_tag_id === ArticleTag::TOP_TAG) && ($article_type->checkHasSubArticleType())) {
            $this->checkCustomValue(false, '一级种类下面有二级种类，不能删除！');
        } else if ($article_type->checkHasArticle()) {
            $this->checkCustomValue(false, '文章种类下面有文章，不能删除！');
        }

        $deleted = $article_type->delete();

        return $deleted;
    }

    /**
     * @param array $article_type_data
     * @param ArticleTag $article_type
     * @return bool|int
     */
    private function updateArticleType(array $article_type_data, ArticleTag $article_type)
    {
        $super_id = $article_type->super_tag_id;

        if (($super_id === ArticleTag::TOP_TAG) && ((int)$article_type_data['super_tag_id'] !== $super_id)) {
            $this->checkCustomValue(false, '一级种类的上级只能是顶级类别');
        } else if (($super_id !== ArticleTag::TOP_TAG) &&
            ((int)$article_type_data['super_tag_id'] === ArticleTag::TOP_TAG)
        ) {
            $this->checkCustomValue(false, '二级种类的上级只能是一级种类');
        }

        $updated = $article_type->update($article_type_data);

        return $updated;
    }

    /**
     * @param ArticleTypeStoreRequest|ArticleTypeUpdateRequest|Request $request
     * @return array
     */
    private function transformArticleTypeData(Request $request)
    {
        return [
            'super_tag_id' => $request->super_tag_id,
            'name'         => $request->name,
            'summary'      => $request->summary?:null,
            'thumb'        => $request->thumb?:null,
            'sort'         => $request->sort?:0,
        ];
    }
}
